﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using BasePlatformCommon;

namespace L9SubSet
{
	public partial class Form_ChipSet : Form
	{
		public string chipName;			// 芯片名称
		public int chipNum = 1;			// 芯片个数
		public int channelNum = 12;		// 芯片管脚数

		private string[] mainColorModeList = { "三色模式", "四色模式" };					// 颜色模式数组
		private string[] subColorModeList = { "禁用", "高亮纯模式", "高亮增强模式" };		// 四色模式状态数组

		public bool binFlag = false;

		private byte[] manualColorArray = new byte[512];

		public int mainColorMode = 0;
		public int subColorMode = 0;

		private int tbWidth = 25;
		private int tbHeight = 20;

		private bool chipJustChanged = false;
		private bool channelManualEnable = false;

		private bool change2ThreeColorMode = false;

		private bool closeByClickButton = false;

		private string section = "ChipSet";			// 多语言文件配置节名称

		private string tip = "提示";					// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_ChipSet(bool chipJustChanged, bool channelManualEnable, bool change2ThreeColorMode)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			this.chipJustChanged = chipJustChanged;
			this.channelManualEnable = channelManualEnable;
			this.change2ThreeColorMode = change2ThreeColorMode;

			if (change2ThreeColorMode == true)
			{
				mainColorMode = 3;
			}

			colorModeCombobox.Items.Clear();
			colorModeCombobox.Items.AddRange(mainColorModeList);
			colorModeCombobox.SelectedIndex = 0;
			colorModeCombobox.SelectedIndexChanged += colorModeCombobox_SelectedIndexChanged;

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "chipSet.Text");
			colorModeCombobox.Items[0] = LanguageOper.ReadValue(section, "mainMode.0");
			colorModeCombobox.Items[1] = LanguageOper.ReadValue(section, "mainMode.1");
			//fourColorStateComboBox.Items[0] = LanguageOper.ReadValue(section, "subMode.0");
			//fourColorStateComboBox.Items[1] = LanguageOper.ReadValue(section, "subMode.1");
			//fourColorStateComboBox.Items[2] = LanguageOper.ReadValue(section, "subMode.2");
			tubePropGroupBox.Text = LanguageOper.ReadValue(section, "tubePropGroupBox.Text");
			//chipTipLabel.Text = LanguageOper.ReadValue(section, "chipTipLabel.Text");
			channelNumTipLabel.Text = LanguageOper.ReadValue(section, "channelNumTipLabel.Text");
			chipNumTipLabel.Text = LanguageOper.ReadValue(section, "chipNumTipLabel.Text");
			colorModeLabel.Text = LanguageOper.ReadValue(section, "colorModeLabel.Text");
			//fourColorLabel.Text = LanguageOper.ReadValue(section, "fourColorLabel.Text"); 
			if (LanguageOper.LocalLanguage != LanguageEnum.EN)
			{
				//channelNumTipLabel.Location = new Point(44, 63);
				//chipNumTipLabel.Location = new Point(32, 96);
				colorModeLabel.Location = new Point(199, 30);
			}
			else
			{
				//channelNumTipLabel.Location = new Point(15, 63);
				//chipNumTipLabel.Location = new Point(32, 96);
				colorModeLabel.Location = new Point(195, 30);
			}
			
			autoColorButton.Text = LanguageOper.ReadValue(section, "autoColorButton.Text");
			clearButton.Text = LanguageOper.ReadValue(section, "clearButton.Text");
			noticeLabel.Text = LanguageOper.ReadValue(section, "noticeLabel.Text");
			channelSetTipLabel.Text = LanguageOper.ReadValue(section, "channelSetTipLabel.Text");
			okButton.Text = LanguageOper.ReadValue(section, "okButton.Text");
			cancelButton.Text = LanguageOper.ReadValue(section, "cancelButton.Text");
		}

		// 界面初始化
		public void initForm()
		{

			// 更新芯片管脚数
			channelNumLabel.Text = channelNum.ToString();
			channelNumComboBox.Text = channelNum.ToString();
			if (channelManualEnable == true)
			{
				for (int i = 0; i < 255; i++)
				{
					channelNumComboBox.Items.Add(i + 1);
				}
				channelNumComboBox.Visible = true;
			}
			else
			{
				channelNumComboBox.Visible = false;
			}

			// 更新芯片个数
			chipNumTextBox.Text = chipNum.ToString();

			//更新芯片颜色模式
			if (mainColorMode == 3)
			{
				colorModeCombobox.SelectedIndex = 0;
				inputTipLabel1.Text = "输入为字母与数字的组合，例如：R1，G12，B14等，";
				inputTipLabel2.Text = "三色模式识别的颜色有红R、绿G、蓝B、空点N";
				if (LanguageOper.available == true)
				{
					inputTipLabel1.Text = LanguageOper.ReadValue(section, "inputTipLabel1.Text.0");
					inputTipLabel2.Text = LanguageOper.ReadValue(section, "inputTipLabel2.Text.0");
				}

				// 初始化自动映射列表
				autoColorContextMenuStrip.Items.Clear();
				autoColorContextMenuStrip.Items.Add("RGB");
				autoColorContextMenuStrip.Items.Add("RBG");
				autoColorContextMenuStrip.Items.Add("BGR");
				autoColorContextMenuStrip.Items.Add("BRG");
				autoColorContextMenuStrip.Items.Add("GRB");
				autoColorContextMenuStrip.Items.Add("GBR");
			}
			else
			{
				colorModeCombobox.SelectedIndex = 1;
				inputTipLabel1.Text = "输入为字母与数字的组合，例如：R1，G12，B14，W2等，";
				inputTipLabel2.Text = "四色模式识别的颜色有红R、绿G、蓝B、白W";
				if (LanguageOper.available == true)
				{
					inputTipLabel1.Text = LanguageOper.ReadValue(section, "inputTipLabel1.Text.1");
					inputTipLabel2.Text = LanguageOper.ReadValue(section, "inputTipLabel2.Text.1");
				}

				// 初始化自动映射列表
				autoColorContextMenuStrip.Items.Clear();
				autoColorContextMenuStrip.Items.Add("RGBW");
				autoColorContextMenuStrip.Items.Add("RBGW");
				autoColorContextMenuStrip.Items.Add("BGRW");
				autoColorContextMenuStrip.Items.Add("BRGW");
				autoColorContextMenuStrip.Items.Add("GRBW");
				autoColorContextMenuStrip.Items.Add("GBRW");
			}

			autoColorContextMenuStrip.ItemClicked += autoColorContextMenuStrip_ItemClicked;

			// 更新管脚颜色映射表格
			updateColorLayoutPanel();
		}

		// 窗体加载后处理：当通道数为 4/8/16 时，若规则排列选择为三色，则高级界面初始化后，应改为三色
		private void Form_ChipSet_Shown(object sender, EventArgs e)
		{
			if (change2ThreeColorMode == true)
			{
				colorModeCombobox.SelectedIndex = 0;
			}
		}

		// 更新管脚颜色映射表格
		private void updateColorLayoutPanel()
		{
			int nRows = 1;
			colorLayoutPanel.Controls.Clear();
			colorLayoutPanel.RowCount = 1;
			colorLayoutPanel.Refresh();
			TableLayoutColumnStyleCollection style_panel = colorLayoutPanel.ColumnStyles;

			// 初始化表格
			foreach (ColumnStyle style in style_panel)
			{
				style.SizeType = SizeType.Absolute;
				style.Width = tbWidth;
			}

			for (int i = 0; i < channelNum * chipNum; i++)
			{
				TextBox tb = new TextBox();
				tb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
				tb.Text = "";
				tb.Margin = new System.Windows.Forms.Padding(0);
				tb.AutoSize = false;
				tb.Height = tbHeight;
				tb.Width = tbWidth;
				tb.TextChanged += tb_TextChanged;
				colorLayoutPanel.Controls.Add(tb, i, 0);
			}

			// 芯片信息显示, 设置显示内容, 判断是否为初始化, 若不为初始化则进行颜色界面显示
			colorLayoutPanel.Controls.Clear();
			int adr = 0;	// 取数据位置
			for (int i = 0; i < nRows; i++)
			{
				for (int j = 0; j < channelNum * chipNum; j++)
				{
					TextBox tb = new TextBox();
					tb.Text = "";
					tb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
					tb.Margin = new System.Windows.Forms.Padding(0);
					tb.AutoSize = false;
					tb.Height = tbHeight;
					tb.Width = tbWidth;
					tb.TextChanged += tb_TextChanged;
					string str;
					Color dwBackColor = Color.Black;
					Color dwtextColor = Color.Black;
					int pix = Convert.ToInt32(manualColorArray[adr]);
					int color = Convert.ToInt32(manualColorArray[adr + 1]);
					if (color == 0 && pix != 0xff)
					{
						str = "R";
						dwBackColor = Color.Red;
					}
					else if (color == 1 && pix != 0xff)
					{
						str = "G";
						dwBackColor = Color.Green;
					}
					else if (color == 2 && pix != 0xff)
					{
						str = "B";
						dwBackColor = Color.Blue;
					}
					else if (color == 3 && pix != 0xff)
					{
						str = "N";
						dwBackColor = Color.White;
						dwtextColor = Color.Black;
					}
					else if (color == 4 && pix != 0xff)
					{
						str = "W";
						dwBackColor = Color.White;
						dwtextColor = Color.Black;
					}
					else if (color == 5 && pix != 0xff)
					{
						str = "Y";
						dwBackColor = Color.Yellow;
					}
					else
					{
						str = "";
						dwBackColor = Color.White;
					}
					tb.BackColor = dwBackColor;
					tb.ForeColor = dwtextColor;
					if (pix == 0xff)
					{
						tb.Text = "";
					}
					else
					{
						tb.Text = str + (pix + 1).ToString();
					}
					colorLayoutPanel.Controls.Add(tb, j, i);
					adr = adr + 2;
				}
			}
		}

		// 颜色模式变化
		private void colorModeCombobox_SelectedIndexChanged(object sender, EventArgs e)
		{
			// 三色模式
			if (colorModeCombobox.SelectedIndex == 0)
			{
				inputTipLabel1.Text = "输入为字母与数字的组合，例如：R1，G12，B14等，";
				inputTipLabel2.Text = "三色模式识别的颜色有红R、绿G、蓝B、空点N";
				if (LanguageOper.available == true)
				{
					inputTipLabel1.Text = LanguageOper.ReadValue(section, "inputTipLabel1.Text.0");
					inputTipLabel2.Text = LanguageOper.ReadValue(section, "inputTipLabel2.Text.0");
				}

				mainColorMode = 3;

				// 初始化自动映射列表
				autoColorContextMenuStrip.Items.Clear();
				autoColorContextMenuStrip.Items.Add("RGB");
				autoColorContextMenuStrip.Items.Add("RBG");
				autoColorContextMenuStrip.Items.Add("BGR");
				autoColorContextMenuStrip.Items.Add("BRG");
				autoColorContextMenuStrip.Items.Add("GRB");
				autoColorContextMenuStrip.Items.Add("GBR");

				// 将点 W 替换为点 N
				for (int i = 0; i < colorLayoutPanel.Controls.Count; i++)
				{
					string strChannelText = colorLayoutPanel.GetControlFromPosition(i, 0).Text;
					if (strChannelText.ToUpper().StartsWith("W"))
					{
						colorLayoutPanel.GetControlFromPosition(i, 0).Text = strChannelText.ToUpper().Replace("W", "N");
					}
				}
			}
			// 四色模式
			else
			{
				inputTipLabel1.Text = "输入为字母与数字的组合，例如：R1，G12，B14等，";
				inputTipLabel2.Text = "四色模式识别的颜色有红R、绿G、蓝B、白W";
				if (LanguageOper.available == true)
				{
					inputTipLabel1.Text = LanguageOper.ReadValue(section, "inputTipLabel1.Text.1");
					inputTipLabel2.Text = LanguageOper.ReadValue(section, "inputTipLabel2.Text.1");
				}

				mainColorMode = 4;

				// 初始化自动映射列表
				autoColorContextMenuStrip.Items.Clear();
				autoColorContextMenuStrip.Items.Add("RGBW");

				// 将点 N 替换为点 W
				for (int i = 0; i < colorLayoutPanel.Controls.Count; i++)
				{
					string strChannelText = colorLayoutPanel.GetControlFromPosition(i, 0).Text;
					if (strChannelText.ToUpper().StartsWith("N"))
					{
						colorLayoutPanel.GetControlFromPosition(i, 0).Text = strChannelText.ToUpper().Replace("N", "W");
					}
				}
			}
		}

		// 单个管脚颜色设置变化
		private void tb_TextChanged(object sender, EventArgs e)
		{
			TextBox tb = (TextBox)sender;
			string input = tb.Text.Trim().ToUpper();

			if (input.StartsWith("R"))
			{
				tb.BackColor = Color.Red;
				tb.ForeColor = Color.Black;
			}
			else if (input.StartsWith("G"))
			{
				tb.BackColor = Color.Green;
				tb.ForeColor = Color.Black;
			}
			else if (input.StartsWith("B"))
			{
				tb.BackColor = Color.Blue;
				tb.ForeColor = Color.White;
			}
			else
			{
				tb.BackColor = Color.White;
				tb.ForeColor = Color.Black;
			}

			tb.Text = input;
			tb.SelectionStart = tb.Text.Length;
		}

		// 检查表单中数据是否正确
		private bool checkInputData()
		{
			bool errorFlag = false;
			int errorIndex = -1;

			// 检查三色模式的数据
			if (mainColorMode == 3)
			{
				for (int i = 0; i < colorLayoutPanel.Controls.Count; i++)
				{
					string colorStr = colorLayoutPanel.GetControlFromPosition(i, 0).Text.ToUpper().Trim();

					if (colorStr.StartsWith("R") || colorStr.StartsWith("G") || colorStr.StartsWith("B") || colorStr.StartsWith("N"))
					{
						colorStr = colorStr.Substring(1);
						if (colorStr.Equals(""))
						{
							continue;
						}
						else
						{
							int value = -1;
							bool res = int.TryParse(colorStr, out value);
							if (value > 0 && value < 256)
							{
								continue;
							}
							else
							{
								errorFlag = true;
								errorIndex = i;
								break;
							}
						}
					}
					else
					{
						errorFlag = true;
						errorIndex = i;
						break;
					}
				}
			}
			// 检查四色模式的数据
			else if (mainColorMode == 4)
			{
				for (int i = 0; i < colorLayoutPanel.Controls.Count; i++)
				{
					string colorStr = colorLayoutPanel.GetControlFromPosition(i, 0).Text.ToUpper().Trim();

					if (colorStr.StartsWith("R") || colorStr.StartsWith("G") || colorStr.StartsWith("B") || colorStr.StartsWith("W"))
					{
						colorStr = colorStr.Substring(1);
						if (colorStr.Equals(""))
						{
							continue;
						}
						else
						{
							int value = -1;
							bool res = int.TryParse(colorStr, out value);
							if (value > 0 && value < 256)
							{
								continue;
							}
							else
							{
								errorFlag = true;
								errorIndex = i;
								break;
							}
						}
					}
					else
					{
						errorFlag = true;
						errorIndex = i;
						break;
					}
				}
			}

			if (errorFlag == true)
			{
				string msg = string.Format("管脚 {0} 颜色输入错误", errorIndex + 1);
				if (LanguageOper.available == true)
				{
					msg = string.Format(LanguageOper.ReadValue(section, "msgStr.0"), errorIndex + 1);
				}
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
			}

			return !errorFlag;
		}

		// 生成管脚排列内部数据
		private bool generalManualColorArray()
		{
			if (checkInputData() == false)
			{
				return false;
			}

			string[] strChannelText = new string[512];
			for (int i = 0; i < colorLayoutPanel.Controls.Count; i++)
			{
				strChannelText[i] = colorLayoutPanel.GetControlFromPosition(i, 0).Text;
			}

			int addr = 0;
			for (int nChannel = 0; nChannel < channelNum * chipNum; nChannel++)
			{
				char colorChar = strChannelText[nChannel][0];

				string channelStr = strChannelText[nChannel].Substring(1);
				if (channelStr.Equals(""))
				{
					channelStr = "1";
				}

				int channelIndex = Convert.ToInt32(channelStr) - 1;
				manualColorArray[addr++] = Convert.ToByte(channelIndex);

				if (colorChar == 'R')
				{
					manualColorArray[addr++] = 0;
				}
				else if (colorChar == 'G')
				{
					manualColorArray[addr++] = 1;
				}
				else if (colorChar == 'B')
				{
					manualColorArray[addr++] = 2;
				}
				else if (colorChar == 'N')
				{
					manualColorArray[addr++] = 3;
				}
				else if (colorChar == 'W')
				{
					manualColorArray[addr++] = 4;
				}
				else if (colorChar == 'Y')
				{
					manualColorArray[addr++] = 5;
				}
			}
			return true;
		}

		// 设置管脚排列内部数据
		public void setManualColorArray(byte[] arrayData)
		{
			for (int i = 0; i < arrayData.Length; i++)
			{
				manualColorArray[i] = arrayData[i];
			}
		}

		// 获取管脚排列内部数据
		public void getManualColorArray(byte[] byteData)
		{
			for (int i = 0; i < manualColorArray.Length; i++)
			{
				byteData[i] = manualColorArray[i];
			}
		}

		// 修改管脚数
		private void channelNumComboBox_TextChanged(object sender, EventArgs e)
		{
			if (channelNumComboBox.Text.Trim().Equals(""))
			{
				return;
			}

			int channelNumValue = -1;
			bool res = int.TryParse(channelNumComboBox.Text.Trim(), out channelNumValue);
			if (res == false || channelNumValue < 1 || channelNumValue > 255)
			{
				string msg = "管脚数输入错误：应在 [1,255] 范围";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.1");
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
				
				return;
			}

			channelNum = channelNumValue;
			for (int i = chipNum * channelNum * 2; i < 512; i++)
			{
				manualColorArray[i] = 0xff;
			}

			// 更新管脚颜色映射表格
			updateColorLayoutPanel();
		}

		// 修改芯片个数
		private void chipNumTextBox_TextChanged(object sender, EventArgs e)
		{
			if (chipNumTextBox.Text.Trim().Equals(""))
			{
				return;
			}

			int chipNumValue = -1;
			bool res = int.TryParse(chipNumTextBox.Text.Trim(), out chipNumValue);
			if (res == false || chipNumValue < 1)
			{
				string msg = "芯片个数应为正整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.2");
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
				
				return;
			}

			if (channelNum * chipNumValue > 256)
			{
				string msg = "管脚总数（管脚数 * 芯片个数）> 256";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.3");
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
				
				return;
			}

			chipNum = chipNumValue;
			for (int i = chipNum * channelNum * 2; i < 512; i++)
			{
				manualColorArray[i] = 0xff;
			}
				
			// 更新管脚颜色映射表格
			updateColorLayoutPanel();
		}

		// 自动映射
		private void autoColorButton_Click(object sender, EventArgs e)
		{
			// 设置 menu 显示位置
			Point pt = new Point(autoColorButton.Location.X, autoColorButton.Location.Y + autoColorButton.Height);
			this.autoColorContextMenuStrip.Show(this.tubePropGroupBox, pt);
			this.BringToFront();
		}

		// 选择自动映射项
		private void autoColorContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			for (int i = 0; i < 512; i++)
			{
				manualColorArray[i] = 0xff;
			}

			string colorStr = e.ClickedItem.Text;
			if (colorStr.Length == 3)
			{
				int addr = 0;
				int channelLoop = 0;
				while (true)
				{
					if (addr >= chipNum * channelNum * 2)
					{
						break;
					}

					for (int i = 0; i < 3; i++)
					{
						manualColorArray[addr] = (byte)channelLoop;
						if (colorStr[i] == 'R')
						{
							manualColorArray[addr + 1] = 0;
						}
						else if (colorStr[i] == 'G')
						{
							manualColorArray[addr + 1] = 1;
						}
						else if (colorStr[i] == 'B')
						{
							manualColorArray[addr + 1] = 2;
						}
						addr += 2;
					}

					channelLoop++;
				}
			}
			else if (colorStr.Length == 4)
			{
				int addr = 0;
				int channelLoop = 0;
				while (true)
				{
					if (addr >= chipNum * channelNum * 2)
					{
						break;
					}

					for (int i = 0; i < 4; i++)
					{
						manualColorArray[addr] = (byte)channelLoop;
						if (colorStr[i] == 'R')
						{
							manualColorArray[addr + 1] = 0;
						}
						else if (colorStr[i] == 'G')
						{
							manualColorArray[addr + 1] = 1;
						}
						else if (colorStr[i] == 'B')
						{
							manualColorArray[addr + 1] = 2;
						}
						else if (colorStr[i] == 'W')
						{
							manualColorArray[addr + 1] = 4;
						}
						addr += 2;
						if (addr >= chipNum * channelNum * 2)
						{
							break;
						}
					}

					channelLoop++;
				}
			}

			// 更新管脚颜色映射表格
			updateColorLayoutPanel();
		}

		// 清除
		private void clearButton_Click(object sender, EventArgs e)
		{
			int allchipspin = chipNum * channelNum;
			for (int index = 0; index < allchipspin; index++)
			{
				colorLayoutPanel.Controls[index].Text = "";
				colorLayoutPanel.Controls[index].BackColor = Color.White;
			}
			for (int i = 0; i < 512; i++)
			{
				manualColorArray[i] = 0xff;
			}
		}

		// 确定
		private void okButton_Click(object sender, EventArgs e)
		{
			closeByClickButton = false;
			bool res = closingHandle();
			if (res == true)
			{
				closeByClickButton = true;
				this.DialogResult = DialogResult.OK;
			}
		}

		// 退出
		private void exitButton_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		// 退出时的处理操作
		private bool closingHandle()
		{
			// 检查数据
			int channelNumValue = -1;
			bool res = false;
			if (channelManualEnable == false)
			{
				res = int.TryParse(channelNumLabel.Text.Trim(), out channelNumValue);
			}
			else
			{
				res = int.TryParse(channelNumComboBox.Text.Trim(), out channelNumValue);
			}
			if (res == false || channelNumValue < 1 || channelNumValue > 256)
			{
				string msg = "管脚数输入错误：应在 [1,255] 范围";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.1");
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
				
				return false;
			}

			int chipNumValue = -1;
			res = int.TryParse(chipNumTextBox.Text.Trim(), out chipNumValue);
			if (res == false || chipNumValue < 1)
			{
				string msg = "芯片个数应为正整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.2");
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

				return false;
			}

			if (channelNumValue * chipNumValue > 256)
			{
				string msg = "管脚总数（管脚数 * 芯片个数）> 256";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.3");
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

				return false;
			}

			channelNum = channelNumValue;
			chipNum = chipNumValue;

			// 初始化任意颜色数据
			for (int i = 0; i < 512; i++)
			{
				manualColorArray[i] = 0xff;
			}
			bool success = generalManualColorArray();
			if (success)
			{
				chipNum = Convert.ToInt32(chipNumTextBox.Text);
				mainColorMode = colorModeCombobox.SelectedIndex + 3;
				return true;
			}
			else
			{
				return false;
			}
		}

		// 窗体关闭前处理
		private void Form_ChipSet_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (closeByClickButton == false)
			{
				bool res = closingHandle();
				if (res == true)
				{
					this.DialogResult = DialogResult.OK;
				}
				else
				{
					e.Cancel = true;
				}
			}
		}
	}
}
